<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Web Solutions
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2006/08/16 11:45:02 $)
// $RCSfile: impex_step2.php,v $ : $Revision: 1.4 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================


if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

if ($dbCore->connected)
{
	$userData = $dbCore->queryFetch("SELECT * FROM `". TABLE_PREFIX ."staff` WHERE `username` = '". addslashes($_POST["username"]) ."' AND `password` = '". addslashes(md5($_POST["password"])) ."';");

	if ($userData["username"] == $_POST["username"] && trim($userData["username"]) != "")
	{
		// Authenticated, Everything is fine.. Now connect to the other database
		$impexDB = new dbCore($_POST["dbhostname"], $_POST["dbusername"], $_POST["dbpassword"], $_POST["dbname"], "mysql", true);
		if (!$impexDB->connected)
		{
			$_SWIFT["error"] = true;
			$_SWIFT["errormsg"] = "Unable to connect to Impex Database.<BR />MySQL Error: ".$impexDB->fetchLastError();
			displayHeader();
			displayError($_SWIFT["errormsg"]);
			displayFooter();
			exit;
		}

		// Get the current version and other settings like registered modules, we only upgrade those modules that are registered
		$Record = $dbCore->queryFetch("SELECT * FROM `". TABLE_PREFIX ."settings` WHERE `section` = 'core' AND `vkey` = 'version';");
		$currentversion = $Record["data"];

		// Now get the registered modules
		$registeredmodules = array();
		$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."settings` WHERE `section` = 'registeredmodules';");
		while ($dbCore->nextRecord())
		{
			$registeredmodules[] = $dbCore->Record["vkey"];
		}
		// Add core module, its not registered anywhere
		$registeredmodules[] = "core";

		if (!is_array($registeredmodules))
		{
			$_SWIFT["error"] = true;
			$_SWIFT["errormsg"] = "Invalid Settings Table, Upgrade Wizard was unable to recover from this error, Please contact support";
		} else if (!in_array("tickets", $registeredmodules)) {
			$_SWIFT["error"] = true;
			$_SWIFT["errormsg"] = "Tickets Module is not registered. This Import/Export script can only import Tickets related data.";
		} else if (empty($_IMPEX[$_POST["product"]])) {
			$_SWIFT["error"] = true;
			$_SWIFT["errormsg"] = "Invalid Import Product.";
		} else {
			loadImpexHash($_POST["hashfilename"]);

			// Ok everything is fine now, we have both the current version and the registered modules
			require_once ($_IMPEX[$_POST["product"]]);

			if (!defined("IMPEX_MODULE_LOADED"))
			{
				$_SWIFT["error"] = true;
				$_SWIFT["errormsg"] = "Unable to load Impex Product Loader.";
			} else if ($_POST["action"] == "-1") {
				$_REQUEST["step"] = "3";
			} else {
				unset($_ACTIONS); unset($_PLUGINS); unset($_CURRENTACTION);
				$_ACTIONS = getImpexActions();
				$_PLUGINS = getImpexPluginFiles();

				// Is the page empty?
				if (empty($_POST["page"]))
				{
					$_POST["page"] = "1";
				}

				// Is the current action empty?
				if (empty($_POST["action"]))
				{
					$_CURRENTACTION = $_ACTIONS[0];
					$_ACTIONINDEX = 0;
				} else {
					$_CURRENTACTION = intval($_ACTIONS[$_POST["action"]]);
					$_ACTIONINDEX = intval($_POST["action"]);
				}

				displayHeader();

				// Load the Action Plugin
				require_once ($_PLUGINS[$_CURRENTACTION]);
				eval('$totalpages = getPluginTotalPages'. $_CURRENTACTION .'();');

				// Display Current Page etc
				displayMessage("Importing: ". $_IMPEXACTIONS[$_CURRENTACTION] ." (Page ". $_POST["page"] ." of $totalpages) - (". strtoupper($_IMPEX[$_POST["product"]]) .")");

				eval('runPluginPage'. $_CURRENTACTION .'($_POST["page"]);');

				// Execute the queries
				execAllQueries();

//				echo $_CURRENTACTION;

				// Do we need to shift to next action?
				if ($_POST["page"] >= $totalpages)
				{
					$_NEXTACTIONINDEX = $_ACTIONINDEX+1;
					$_NEXTACTION = $_ACTIONS[$_NEXTACTIONINDEX];
//					echo $_IMPEXACTIONS[$_NEXTACTION];
					if (empty($_NEXTACTION))
					{
						// No action? Import done!
						$_SWIFT["execpage"] = "0";
						$_SWIFT["execaction"] = "-1";
					} else {
						// We need to move to next action
						$_SWIFT["execaction"] = $_NEXTACTIONINDEX;
						$_SWIFT["execpage"] = "1";
					}
				} else {
					// There is another page to execute
					$_SWIFT["execaction"] = $_ACTIONINDEX;
					$_SWIFT["execpage"] = $_POST["page"]+1;
				}

				displayFooter();

				saveImpexHash($_POST["hashfilename"]);
			}
		}
	} else {
		$_POST["step"] = "1";
		$_SWIFT["errormsg"] = true;
		$_SWIFT["errormsg"] = "Invalid Username or Password";
	}
} else {
	$_SWIFT["error"] = true;
}
?>